//
//  KSAdDrawDemoCell.m
//  KSAdSDKGitDemo
//
//  Created by xuzhijun on 2019/12/6.
//  Copyright © 2019 xuzhijun. All rights reserved.
//

#import "KSAdDrawDemoCell.h"
#import <KSAdSDK/KSAdSDK.h>
#import "KSAdPlayerView.h"

@interface KSAdDrawDemoCell () <KSAdBasePlayerViewDelegate>

@property (nonatomic, strong) KSAdPlayerView *playerView;
//@property (nonatomic, strong) KSAdPlayerCache1View *playerView;
@property (nonatomic, strong) KSAdDrawModel *model;

@property (nonatomic, strong) UILabel *topLabel;
@property (nonatomic, strong) UILabel *bottomLabel;

@end

@implementation KSAdDrawDemoCell

#pragma mark - Constructor And Destructor
- (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier {
    if (self = [super initWithStyle:style reuseIdentifier:reuseIdentifier]) {
        [self.contentView addSubview:self.topLabel];
        [self.contentView addSubview:self.bottomLabel];
        
        self.topLabel.frame = CGRectMake(60, 64, self.contentView.frame.size.width-60, 40);

        self.bottomLabel.frame = CGRectMake(60, self.contentView.frame.size.height-60, self.contentView.frame.size.width-60, 40);
        
    }
    return self;
}

#pragma mark - Public
- (void)refreshWithModel:(KSAdDrawModel *)model {
    self.model = model;
    self.topLabel.text = [NSString stringWithFormat:@"Draw top 测试文案：%zd", (long)model.index];
    self.bottomLabel.text = [NSString stringWithFormat:@"Draw bottom 测试文案：%zd", (long)model.index];
    [self.playerView stop];
    [self.playerView removeFromSuperview];
    [self.contentView insertSubview:self.playerView atIndex:0];
    self.playerView.frame = self.contentView.bounds;
}

- (void)play {
    [self.playerView setURL:[NSURL URLWithString:self.model.videoUrl]];
    [self.playerView play];
}
- (void)pause {
    [self.playerView pause];
}

- (NSUInteger)cellIndex {
    return [self.model.videoUrl hash];
}

#pragma mark - UI Getter
//- (KSAdPlayerCache1View *)playerView {
//    if (_playerView == nil) {
//        _playerView = [KSAdPlayerCache1View new];
////        _playerView.delegate = self;
//    }
//    return _playerView;
//}

- (KSAdPlayerView *)playerView {
    if (_playerView == nil) {
        _playerView = [KSAdPlayerView new];
        _playerView.videoSoundEnable = YES;
//        _playerView.delegate = self;
    }
    return _playerView;
}
- (UILabel *)topLabel {
    if (_topLabel == nil) {
        _topLabel = [UILabel new];
        _topLabel.backgroundColor = [UIColor clearColor];
        _topLabel.numberOfLines = 1;
        _topLabel.textColor = [UIColor redColor];
        _topLabel.textAlignment = NSTextAlignmentLeft;
    }
    return _topLabel;
}
- (UILabel *)bottomLabel {
    if (_bottomLabel == nil) {
        _bottomLabel = [UILabel new];
        _bottomLabel.backgroundColor = [UIColor clearColor];
        _bottomLabel.numberOfLines = 1;
        _bottomLabel.textColor = [UIColor redColor];
        _bottomLabel.textAlignment = NSTextAlignmentLeft;
    }
    return _bottomLabel;
}
@end
